package yuan.zk.hello;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Demo {
    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(1);
        try {
            ZooKeeper zk = new ZooKeeper("127.0.0.1:2181",
                    4000,
                    new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            if(Event.KeeperState.SyncConnected==event.getState()){
                                //如果收到了服务端的响应事件，连接成功
                                latch.countDown();
                            }
                        }
                    }
            );
            latch.await();
            System.out.println(zk.getState());
            System.out.println("zzz");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
